<?php
/**
 * 账单组
 * @author solar
 */
class GroupProxy extends BaseProxy
{
	/**
	 * 组成员键值
	 * @author solar
	 */
	public function memberKV() {
		$validator=new Validator();
		$validator->setRule('group_id',array('账单组ID','required','int'=>array('isPositive'=>true)));
		$validation=$validator->validate($_GET);
		if($validation!==true) return $this->fail($validation);
		return $this->success(ModelFactory::get('GroupUserModel')->getMemberKV($_GET['group_id']));
	}
	
	/**
	 * 我的账单组
	 * @author solar
	 */
	public function mine() {
		$data['list'] = ModelFactory::get('GroupModel')->getListByUser(CoreFactory::session()->get('user_id'));
		return $this->success($data);
	}
	
	/**
	 * 账单组成员列表
	 * @author solar
	 */
	public function members() {
		$validator=new Validator();
		$validator->setRule('group_id',array('账单组ID','required','int'=>array('isPositive'=>true)));
		$validation=$validator->validate($_GET);
		if($validation!==true) return $this->fail($validation);
		$groupModel = ModelFactory::get('GroupModel');
		$groupRow = $groupModel->getActive($_GET['group_id']);
		if(empty($groupRow)) $this->fail('无效的账单组ID');
		$result['creator'] = $groupRow['g_creator'];
		$result['is_creator'] = $groupModel->isCreator($_GET['group_id'])===true ? 1 : 0;
		$result['list'] = ModelFactory::get('GroupUserModel')->listGroupMember($_GET['group_id']);
		return $this->success($result);
	}
	
	/**
	 * 创建账单组
	 * @author solar
	 */
	public function create() {
		$validator=new Validator();
		$validator->setRule('group_name',array('组名','required','length'=>array('max'=>50)));
		$validation=$validator->validate($_POST);
		if($validation!==true) return $this->fail($validation);
		$user_id = CoreFactory::session()->get('user_id');
		$group_id=ServiceFactory::get('GroupService')->createBase($_POST['group_name'], $user_id);		
		if($group_id===false) return $this->error(Gateway::InternalError);
		$groupUserModel = ModelFactory::get('GroupUserModel');
		$groupUserModel->setRandomDefault($user_id);
		$groupRow = ModelFactory::get('GroupModel')->get($group_id);
		$default_id =  $groupUserModel->getDefaultGroup($user_id);
		$groupRow['is_default'] = $group_id==$default_id ? 1 : 0; 
		return $this->success($groupRow);
	}
	
	/**
	 * 修改账单组
	 * @author solar
	 */
	public function modify() {
		$validator=new Validator();
		$validator->setRule('group_id',array('账单组ID','required','int'=>array('isPositive'=>true)));
		$validator->setRule('group_name',array('组名','required','length'=>array('max'=>50)));
		$validation=$validator->validate($_POST);
		if($validation!==true) return $this->fail($validation);
		$groupModel=ModelFactory::get('GroupModel');
		$groupRow=$groupModel->getActive($_POST['group_id']);
		if(empty($groupRow)) return $this->fail('找不到账单组['.$_POST['group_id'].']'); 
		if($groupModel->isCreator($_POST['group_id'])===false)
			return $this->fail('您不是账单组的创建者，没有修改权限');
		$aUpdate['g_name'] = $_POST['group_name'];
		$result=$groupModel->update($aUpdate,$_POST['group_id']);
		if(!$result) return $this->error(Gateway::InternalError);
		return $this->success($_POST);
	}
	
	/**
	 * 修改账单组名称
	 * @author solar
	 */
	public function rename() {
		$validator=new Validator();
		$validator->setRule('group_id',array('账单组ID','required','int'=>array('isPositive'=>true)));
		$validator->setRule('new_name',array('新账单组名称','required','length'=>array('max'=>50)));
		$validation=$validator->validate($_POST);
		if($validation!==true) return $this->fail($validation);
		$groupModel=ModelFactory::get('GroupModel');
		$groupRow=$groupModel->getActive($_POST['group_id']);
		if(empty($groupRow)) return $this->fail('找不到账单组['.$_POST['group_id'].']'); 
		if($_POST['new_name']===$groupRow['g_name'])
			return $this->fail('账单组名称一样，不用修改');
		if($groupModel->isCreator($_POST['group_id'])===false)
			return $this->fail('您不是账单组的创建者，不能修改账单组名称');
		$result=$groupModel->changeName($_POST['group_id'],$_POST['new_name']);
		if(!$result) return $this->error(Gateway::InternalError);
		return $this->success();
	}
	
	/**
	 * 设置默认分组
	 * @author solar
	 */
	public function setDefault() {
		$validator=new Validator();
		$validator->setRule('group_id',array('账单组ID','required','int'=>array('isPositive'=>true)));
		$validation=$validator->validate($_POST);
		if($validation!==true) return $this->fail($validation);
		$groupRow = ModelFactory::get('GroupModel')->getActive($_POST['group_id']);
		if(empty($groupRow)) return $this->fail('找不到账单组['.$_POST['group_id'].']');
		$user_id = CoreFactory::session()->get('user_id');
		$groupUserModel = ModelFactory::get('GroupUserModel');
		$defaultRow = $groupUserModel->getDefaultRow($user_id);
		if($defaultRow){
			$groupUserModel->update(array('is_default'=>0),$defaultRow['gu_id']);
			$data['older'] = $defaultRow['g_id'];
		} else $data['older'] = 0;	
		$result = $groupUserModel->setDefaultGroup($user_id, $_POST['group_id']);
		if($result){			
			$data['newer'] = $_POST['group_id'];
			return $this->success($data);
		} else return $this->fail();
	}
	
	/**
	 * 账单组添加新浪微博成员
	 * @author solar
	 */
	public function addSinaUser() {
		$validator=new Validator();
		$validator->setRule('group_id',array('账单组ID','required','int'=>array('isPositive'=>true)));
		$validator->setRule('sina_id',array('新浪微博UID','required','int'=>array('isPositive'=>true)));
		$validation=$validator->validate($_POST);
		if($validation!==true) return $this->fail($validation);
		$groupRow=ModelFactory::get('GroupModel')->getActive($_POST['group_id']);
		if(!$groupRow) return $this->fail('找不到账单组['.$_POST['group_id'].']');
		if($groupRow['g_quantity']==100) return $this->fail('账单组不能超过100位成员');
		$userRow=ServiceFactory::get('UserService')->checkSinaUser($_POST['sina_id']);
		if(!$userRow) return $this->error(Gateway::InternalError);
		$groupUserModel=ModelFactory::get('GroupUserModel');
		if($groupUserModel->inGroup($userRow['u_id'],$_POST['group_id']))
			return $this->fail('账单组中已存在此用户');
		$groupService = ServiceFactory::get('GroupService');
		if($groupService->addMember($_POST['group_id'],$userRow['u_id'])){			
			return $this->success(array('user_id'=>$userRow['u_id']));
		}else return $this->error(Gateway::InternalError);
	}
	
	/**
	 * 账单组添加临时成员
	 * @author solar
	 */
	public function addTempUser() {
		$validator=new Validator();
		$validator->setRule('group_id',array('账单组ID','required','int'=>array('isPositive'=>true)));
		$validator->setRule('temp_name',array('临时成员','required','length'=>array('max'=>50)));
		$validation=$validator->validate($_POST);
		if($validation!==true) return $this->fail($validation);
		$groupRow=ModelFactory::getActive('GroupModel')->get($_POST['group_id']);
		if(!$groupRow) return $this->fail('找不到账单组['.$_POST['group_id'].']');
		if($groupRow['g_quantity']==100) return $this->fail('账单组不能超过100位成员');
		$user_id=ModelFactory::get('UserModel')->createAppUser(0,$_POST['temp_name']);
		if(!$user_id) return $this->error(Gateway::InternalError);
		ModelFactory::get('GroupUserModel')->create($_POST['group_id'],$user_id);
		ModelFactory::get('UserModel')->setDefaultGroup($user_id, $_POST['group_id']);
		return $this->success(array('user_id'=>$user_id));
	}
	
	/**
	 * 移除账单组成员
	 * @author solar
	 */
	public function removeMember() {		
		$validator=new Validator();
		$validator->setRule('group_id',array('账单组ID','required','int'=>array('isPositive'=>true)));
		$validator->setRule('user_id',array('用户ID','required','int'=>array('isPositive'=>true)));
		$validation=$validator->validate($_POST);
		if($validation!==true) return $this->fail($validation);
		if(ModelFactory::get('GroupModel')->isCreator($_POST['group_id'])===false){
			return $this->fail('您不是创建者，没有移除权限');
		}
		if(CoreFactory::session()->get('user_id')==$_POST['user_id']){
			return $this->fail('您不能移除自己');
		} 
		$groupUserModel=ModelFactory::get('GroupUserModel');
		if(!$groupUserModel->inGroup($_POST['user_id'],$_POST['group_id'])){
			return $this->fail('成员不在账单组中');
		}
		if(ServiceFactory::get('BillService')->inUncheckoutBill($_POST['user_id'],$_POST['group_id'])){
			return $this->fail('该成员在群组中尚有未结账单，暂时不能移除');
		}
		$groupService = ServiceFactory::get('GroupService');
		$result = $groupService->removeMember($_POST['group_id'],$_POST['user_id']);
		ServiceFactory::get('UserService')->checkHasGroup($_POST['user_id']);
		if($result === true) return $this->success();
		else return $this->error(Gateway::InternalError);
	}
	
	/**
	 * 退出账单组
	 * @author solar
	 */
	public function exitGroup() {
		$validator=new Validator();
		$validator->setRule('group_id',array('账单组ID','required','int'=>array('isPositive'=>true)));
		$validation=$validator->validate($_POST);
		if($validation!==true) return $this->fail($validation);
		$user_id = CoreFactory::session()->get('user_id');
		$groupUserModel = ModelFactory::get('GroupUserModel');
		if(!$groupUserModel->inGroup($user_id,$_POST['group_id'])){
			return $this->fail('您不在账单组中，不用退出');
		}
		$groupNum = $groupUserModel->countGroup($user_id);
		if($groupNum<2) return $this->fail('您至少要保留一个群组');
		if(ServiceFactory::get('BillService')->inUncheckoutBill($user_id, $_POST['group_id'])){
			return $this->fail('你在群组中尚有未结束账单，暂时不能退出');
		}
		$groupService = ServiceFactory::get('GroupService');
		$result = $groupService->removeMember($_POST['group_id'],$user_id);
		if($result === true) return $this->success();
		else return $this->error(Gateway::InternalError);		
	}
	
	/**
	 * 删除账单组
	 * @author solar
	 */
	public function removeGroup() {
		$validator=new Validator();
		$validator->setRule('group_id',array('账单组ID','required','int'=>array('isPositive'=>true)));
		$validation=$validator->validate($_POST);
		if($validation!==true) return $this->fail($validation);
		if(ModelFactory::get('GroupModel')->isCreator($_POST['group_id'])===false){
			return $this->fail('您不是创建者，没有删除权限');
		}
		$user_id = CoreFactory::session()->get('user_id');
		$groupUserModel = ModelFactory::get('GroupUserModel');
		$groupNum = $groupUserModel->countGroup($user_id);
		if($groupNum < 2) return $this->fail('您至少要保留一个群组');
		$groupService = ServiceFactory::get('GroupService');
		$is = $groupService->hasUnCheckout($_POST['group_id']);
		if($is === true) return $this->fail('群组中尚有未结账单，请结束账单后再删除');
		$groupService->inactiveGroup($_POST['group_id']);
		return $this->success();
	}
		
}
